package shelly.test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: Shelly
 * @create: 2024-02-28 13:36:11
 * @version: 1.0
 * @describe:
 */
public class GenerateTxt {
    public static void main(String[] args) {
        String content = "这是要保存到.txt文件中的内容"; // 修改此处为想要保存的内容
        String fileName = "1";
        method1(fileName,content);
    }

    public static void method1(String fileName,String content) {
        FileWriter fw = null;
        try {
            //如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f=new File("D:\\file\\txt\\"+fileName+".txt");
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(content);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
